package cn.iocoder.yudao.module.tms.controller.admin.order;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.tms.controller.admin.order.vo.*;
import cn.iocoder.yudao.module.tms.dal.dataobject.order.OrderDO;
import cn.iocoder.yudao.module.tms.dal.dataobject.companybaseinfo.CompanyBaseInfoDO;
import cn.iocoder.yudao.module.tms.service.order.OrderService;
import cn.iocoder.yudao.module.tms.service.order.OrderRefImageService;
import cn.iocoder.yudao.module.tms.service.companybaseinfo.CompanyBaseInfoService;

@Tag(name = "管理后台 - 公司订单信息")
@RestController
@RequestMapping("/tms/order-info")
@Validated
public class OrderController {

    @Resource
    private OrderService orderService;

    @Resource
    private OrderRefImageService orderRefImageService;

    @Resource
    private CompanyBaseInfoService companyBaseInfoService;

    @PostMapping("/create")
    @Operation(summary = "创建公司订单信息")
    @PreAuthorize("@ss.hasPermission('tms:order-info:create')")
    public CommonResult<Long> createOrder(@Valid @RequestBody OrderSaveReqVO createReqVO) {
        return success(orderService.createOrder(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新公司订单信息")
    @PreAuthorize("@ss.hasPermission('tms:order-info:update')")
    public CommonResult<Boolean> updateOrder(@Valid @RequestBody OrderSaveReqVO updateReqVO) {
        orderService.updateOrder(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除公司订单信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('tms:order-info:delete')")
    public CommonResult<Boolean> deleteOrder(@RequestParam("id") Long id) {
        orderService.deleteOrder(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除公司订单信息")
    @PreAuthorize("@ss.hasPermission('tms:order-info:delete')")
    public CommonResult<Boolean> deleteOrderList(@RequestParam("ids") List<Long> ids) {
        orderService.deleteOrderListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得公司订单信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('tms:order-info:query')")
    public CommonResult<OrderRespVO> getOrder(@RequestParam("id") Long id) {
        OrderDO order = orderService.getOrder(id);
        OrderRespVO respVO = BeanUtils.toBean(order, OrderRespVO.class);
        // 设置订单图片列表
        respVO.setOrderPhoto(orderRefImageService.getImageUrlsByOrderId(id));
        // 设置公司简称
        if (order.getCompanyId() != null) {
            CompanyBaseInfoDO company = companyBaseInfoService.getCompanyBaseInfo(order.getCompanyId());
            if (company != null) {
                respVO.setCompanyShortName(company.getShortName());
            }
        }
        return success(respVO);
    }

    @GetMapping("/page")
    @Operation(summary = "获得公司订单信息分页")
    @PreAuthorize("@ss.hasPermission('tms:order-info:query')")
    public CommonResult<PageResult<OrderRespVO>> getOrderPage(@Valid OrderPageReqVO pageReqVO) {
        PageResult<OrderDO> pageResult = orderService.getOrderPage(pageReqVO);
        PageResult<OrderRespVO> respPageResult = BeanUtils.toBean(pageResult, OrderRespVO.class);
        // 设置订单图片列表和公司简称
        List<OrderRespVO> list = respPageResult.getList();
        for (OrderRespVO respVO : list) {
            respVO.setOrderPhoto(orderRefImageService.getImageUrlsByOrderId(respVO.getId()));
            // 设置公司简称
            if (respVO.getCompanyId() != null) {
                CompanyBaseInfoDO company = companyBaseInfoService.getCompanyBaseInfo(respVO.getCompanyId());
                if (company != null) {
                    respVO.setCompanyShortName(company.getShortName());
                }
            }
        }
        return success(respPageResult);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出公司订单信息 Excel")
    @PreAuthorize("@ss.hasPermission('tms:order-info:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportOrderExcel(@Valid OrderPageReqVO pageReqVO,
            HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<OrderDO> list = orderService.getOrderPage(pageReqVO).getList();
        // 转换为 VO 并设置公司简称
        List<OrderRespVO> voList = BeanUtils.toBean(list, OrderRespVO.class);
        for (OrderRespVO respVO : voList) {
            if (respVO.getCompanyId() != null) {
                CompanyBaseInfoDO company = companyBaseInfoService.getCompanyBaseInfo(respVO.getCompanyId());
                if (company != null) {
                    respVO.setCompanyShortName(company.getShortName());
                }
            }
        }
        // 导出 Excel
        ExcelUtils.write(response, "公司产品信息.xls", "数据", OrderRespVO.class, voList);
    }

}